package com.sinosoft.ptas.control;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.api.domain.PmsData;
import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.control.dto.ReportData;
import com.sinosoft.ptas.control.dto.ReportRemindData;
import com.sinosoft.ptas.control.dto.RetrieveReportData;
import com.sinosoft.ptas.control.dto.TargetsOfImplphaseData;
import com.sinosoft.ptas.entity.ReportEntity;
import com.sinosoft.ptas.entity.TargetsOfImplPhaseEntity;
import com.sinosoft.ptas.service.ReportService;
import com.sinosoft.ptas.system.commom.CommonConstant;
import com.sinosoft.ptas.system.commom.ExceptionConstant;
import com.sinosoft.ptas.system.commom.PtasConstant;
import com.sinosoft.ptas.system.security.SecurityUtil;
import com.sinosoft.ptas.system.util.EntityListConverter;
import com.sinosoft.ptas.system.util.ResponseMapData;

/**
 * 申报信息控制层
 * @author liufupeng
 *
 */
@Controller
@RequestMapping("/report")
public class ReportController {
	
	@Autowired
	private ReportService reportService;
	
	/**
	 * 添加申报表
	 * @param reportData
	 * @return
	 */
	@RequestMapping(value="/createReport.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData createReport(ReportData reportData){
		
		if(reportData==null){
			return new ResponseData(false,null);
		}
		try {
			reportData.setCommitTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			ReportEntity entity = reportData.toEntity();
			PmsData pmsData = (PmsData)SecurityUtil.getSessionValue(PtasConstant.pms_app_content);
			entity.setDivisionCode(pmsData.getOcode());
			String id = reportService.createReport(entity);
			if(!StringUtil.isBlank(id)){
				reportData.setId(id);
				return new ResponseData(true,reportData);	
			}
			return new ResponseData(false,null);
		} catch (BusinessException e) {
			System.out.println(e.getMessage());
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);
		}
	}
	
	/**
	 * 更新申报表
	 * @param reportData
	 * @return
	 */
	@RequestMapping(value="/updateReport.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateData(ReportData reportData){
		if(reportData==null){
			return new ResponseData(false,null);
		}
		ReportEntity reportEntity = reportService.retrieveReportById(reportData.getId());
		if(reportEntity==null){
			return new ResponseData(false,PtasConstant.ptas_app_error_message);
		}
		try {
			reportData.setCommitTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			reportService.updateReport(reportData.toEntity());
			return new ResponseData(true,reportData.getId());
		} catch (BusinessException e) {
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);
		}
	}
	
	/**
	 * 查询申报表全部显示(多条建查询)
	 * @return
	 */
	@RequestMapping(value="/retrieveReport.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveReportEntity(RetrieveReportData retrieveReportDto){
		PmsData pmsData = (PmsData)SecurityUtil.getSessionValue(PtasConstant.pms_app_content);
		retrieveReportDto.setDivisionCode(pmsData.getOcode());
		List<ReportEntity> reportEntitys = reportService.retrieveReport(retrieveReportDto);
		List<ReportData>  reportData=new ArrayList<>();
		for(int i=0;i<reportEntitys.size();i++){
			ReportEntity reportEntity = reportEntitys.get(i);
			ReportData reportD = toReportData(reportEntity);
			
			//redata.setCommitTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(reportEntity.getCommitTime()));
			//redata.setTargetPhaseDataList(reportEntity.getTargetPhaseList());
			//redata.setAuditData(reportEntity.getAuditList());
			reportData.add(reportD);
		}
		return new ResponseData(true,reportData);
	}
	
	/**
	 * 查询申报表全部显示(多条建查询)
	 * @return
	 */
	@RequestMapping(value="/retrieveReportForDOPAF.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveReportForDOPAF(RetrieveReportData retrieveReportDto){
		List<ReportEntity> reportEntitys = reportService.retrieveReportForDOPAF(retrieveReportDto);
		List<ReportData>  reportData=new ArrayList<>();
		for(int i=0;i<reportEntitys.size();i++){
			ReportEntity reportEntity = reportEntitys.get(i);
			ReportData reportD = toReportData(reportEntity);
			reportData.add(reportD);
		}
		return new ResponseData(true,reportData);
	}
	
	/**
	 * 去重查询省级主管部门
	 * @return provincialGovernments
	 */
	@RequestMapping(value="/distinctProvincialGovernment.service",method=RequestMethod.GET)
	@ResponseBody
	public ResponseData distinctProvincialGovernment(){
		List<ReportEntity> provincialGovernments=reportService.distinctProvincialGovernment();
		return new ResponseData(true,provincialGovernments);
	}
	
	/**
	 * 规財是查询总体实施期目标
	 * @return
	 */
	@RequestMapping(value="/retrieveReportForDOPAF2.service",method=RequestMethod.GET)
	@ResponseBody
	public ResponseData retrieveReportForDOPAF2(){
		PmsData pmsData = (PmsData)SecurityUtil.getSessionValue(PtasConstant.pms_app_content);
		List<ReportEntity> reportEntitys=reportService.retrieveReportForDOPAF2(pmsData.getOcode());
		List<ReportData> reportDatas=getReportDatas(reportEntitys);
		return new ResponseData(true,reportDatas);
	}
	
	/**
	 * 其他司和财政部查看申报信息表
	 * @return
	 */
	@RequestMapping(value="/retrieveReportForOtherDepartment.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveReportForOtherDepartment(RetrieveReportData retrieveReportData){
		List<ReportEntity> reportEntitys=reportService.retrieveReportForOtherDepartment(retrieveReportData);
		List<ReportData> reportDatas=getReportDatas(reportEntitys);
		return new ResponseData(true,reportDatas);
	}
	/**
	 * 根据区域编号检索申报
	 * @param divisionCode
	 * @return reportDatas
	 */
	@RequestMapping(value="/retrieveReportsByDIVISIONCODE.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveReportsByDIVISIONCODE(String divisionCode){
		if(StringUtil.isBlank(divisionCode)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		List<ReportEntity> reportEntitys=reportService.retrieveReportsByDIVISIONCODE(divisionCode);
		List<ReportData> reportDatas=getReportDatas(reportEntitys);
		return new ResponseData(true,reportDatas.get(0));
	}
	/**
	 * 查询每个省的默认实施期目标的默认参考值是否设置完成
	 * @return
	 */
	@RequestMapping(value="/retrieveReportsForRemind.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMapData retrieveReportsForRemind(){
		
		List<ReportRemindData> reportReminds = reportService.retrieveReportsForRemind(CommonConstant.REFERVALUECONSTANT);
		Map<String, List<?>> map = EntityListConverter.toMap("com.sinosoft.ptas.control.dto.ReportRemindData", reportReminds, "serialVersionUID");
		return new ResponseMapData(true, "成功", map);
		
	}
	/**
	 * 其他司查询对应省局的默认目标
	 * @return
	 */
	@RequestMapping(value="/retrieveReportForOtherDepartment2.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveReportForOtherDepartment(){
		PmsData pmsData = (PmsData)SecurityUtil.getSessionValue(PtasConstant.pms_app_content);
		List<ReportEntity> reportEntitys = reportService.retrieveReportForOtherDepartment(pmsData.getOid());
		List<ReportData> reportDatas=getReportDatas(reportEntitys);
		
		return new ResponseData(true,reportDatas);
		
	}
	
	/**
	 * 其他司查询对应省局的默认目标
	 * @return
	 */
	@RequestMapping(value="/retrieveOtherDepartmentTargetPhaseByReportIdAndOrgId",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveOtherDepartmentTargetPhaseByReportIdAndOrgId(String reportId){
		PmsData pmsData = (PmsData)SecurityUtil.getSessionValue(PtasConstant.pms_app_content);
		List<TargetsOfImplPhaseEntity> TargetsOfImplphaseDataEntitys = reportService.retrieveOtherDepartmentTargetPhaseByReportIdAndOrgId(reportId,pmsData.getOid());
		List<TargetsOfImplphaseData> TargetsOfImplphaseData=getTargetsOfImplPhaseDatas(TargetsOfImplphaseDataEntitys);
		return new ResponseData(true,TargetsOfImplphaseData);
		
	}
	
	
	
	/**
	 * @param indexEntity
	 * @return 指标实体类转化为传输类
	 */
	private ReportData toReportData(ReportEntity reportEntity) {
		ReportData reportData = new ReportData();
		reportData.toreportData(reportEntity);
		return reportData;
	}
	
	/**
	 * 申报信息转换格式集合转换
	 * 
	 * @param dict
	 * @return 申报信息传输类
	 */
	private List<ReportData> getReportDatas(List<ReportEntity> reportEntitys) {
		int size = reportEntitys.size();
		List<ReportData> reportDatas = new ArrayList<ReportData>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			ReportData reportData = new ReportData();
			reportData.toreportData(reportEntitys.get(i));
			reportDatas.add(reportData);
		}
		return reportDatas;
	}
	
	/**
	 * 实施期目标格式集合转换
	 * 
	 * @param dict
	 * @return 实施期目标传输类
	 */
	private List<TargetsOfImplphaseData> getTargetsOfImplPhaseDatas(List<TargetsOfImplPhaseEntity> targetsOfImplPhaseEntitys) {
		int size = targetsOfImplPhaseEntitys.size();
		List<TargetsOfImplphaseData> targetsOfImplphaseDatas = new ArrayList<TargetsOfImplphaseData>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			TargetsOfImplphaseData targetsOfImplPhaseData = new TargetsOfImplphaseData();
			targetsOfImplPhaseData.totargetsOfImplPhaseData(targetsOfImplPhaseEntitys.get(i));
			targetsOfImplphaseDatas.add(targetsOfImplPhaseData);
		}
		return targetsOfImplphaseDatas;
	}
}
